package perso;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.HashSet;


import modele.Graphe;
import module.ModuleMobilePerso;
import module.ModuleStateImp;
import module.ModuleStateImpG;
import module.ModuleStateImpU;
import motor.BattleField;
import motor.Motor;
import motor.MotorPerso;
import motor.Unite;
import motor.UniteArmee;
import motor.UniteBatiment;
import motor.UniteCivil;
import utils.Vector2d;
import actions.Action;
import actions.Deplacement;

public class MotorPersoImp implements MotorPerso
{
	Motor motor;

	public Graphe graphe;
	
	public ArrayList<Groupe> populationPerso = new ArrayList<Groupe>();

	ArrayList<Action> actions = new ArrayList<Action>();
	
	ArrayList<Unite> ennemi = new ArrayList<Unite>();
	
	Unite batimentPrinc;
	
	int camp = 1;
	
	public MotorPersoImp( Motor motor, int camp )
	{
		this.camp = camp;
		
		graphe = new Graphe( motor.s );
		
		this.motor = motor ;
		int nbGroupe = 3;
		int nbUnite = 20;
		int rayon = 15;
		int force = 4;
		
		Groupe g;
		Unite u;
		
		/*
		if( camp == 2 )
		{
			g = new Groupe();
			u = new UniteArmee(200,200, camp );
			u.setMstate(new ModuleStateImpU( u , ModuleStateImp.TOURELLE ) );

			g.add(u);
			g.setMs( new ModuleStateImpG( g , ModuleStateImp.GROUPE_STRUCT ));
			motor.ajoutUnite(u);
			populationPerso.add(g);
		}
		
		if( camp == 1 )
		{	
			g = new Groupe();
	
			g.setMs( new ModuleStateImpG( g , ModuleStateImp.GROUPE_CIVIL ));
			
			
			u = new UniteCivil(250,350,camp);
			u.setMstate(new ModuleStateImpU(u, ModuleStateImp.CIVIL ));
			u.setMobile(new ModuleMobilePerso( u ));
			g.add(u);
			motor.ajoutUnite(u);
			
			u = new UniteCivil(250,355,camp);
			u.setMstate(new ModuleStateImpU(u, ModuleStateImp.CIVIL ));
			u.setMobile(new ModuleMobilePerso( u ));
			g.add(u);
			motor.ajoutUnite(u);
			
			u = new UniteCivil(255,350,camp);
			u.setMstate(new ModuleStateImpU(u, ModuleStateImp.CIVIL ));
			u.setMobile(new ModuleMobilePerso( u ));
			g.add(u);
			motor.ajoutUnite(u);
			
			u = new UniteCivil(245,350,camp);
			u.setMstate(new ModuleStateImpU(u, ModuleStateImp.CIVIL ));
			u.setMobile(new ModuleMobilePerso( u ));
			g.add(u);
			motor.ajoutUnite(u);
			
			populationPerso.add(g);
			
			Deplacement dep = new Deplacement( graphe , new Vector2d( 250 , 10 ) );
			
			dep.setCible(g);
			
			dep.initialisation();
			
			g.setAction(dep);
		}
		*/
		
		/*
		for( int i  = 0 ; i < nbGroupe ; i++ )
		{

			Groupe g = new Groupe();
			
			g.setBary( new Vector2d( (float)Math.random() * BattleField.viewer_xsize , (float) (Math.random() * BattleField.viewer_ysize) ) );
			
			
			g.setVtemp(new Vector2d( (float)Math.random() * force*2 -force, (float) (Math.random() * force*2 -force) ) );
			

			for( int j = 0 ; j < nbUnite ; j++ )
			{
				float x = (float)(g.bary.x + Math.random() * rayon * 2 - rayon);
				float y = (float) (g.bary.y + Math.random() * rayon * 2 - rayon);
				
				Unite u = new UniteM( x , y );
				
				u.setVtemp(new Vector2d( (float)Math.random() * force*2 -force, (float) (Math.random() * force*2 -force) ) );
				
				g.add(u);
				
				motor.populationOfficielle.add(u);
			}

			populationPerso.add(g);
		}
		*/
	}

	public void draw(Graphics buffer_canvas)
	{
		if( camp == 1 )
			graphe.draw(buffer_canvas);
		
		for( int i = 0 ; i < populationPerso.size() ; i++ )
		{
			populationPerso.get(i).draw( buffer_canvas );
		}
	}

	public void goOn()
	{
		ennemi = new ArrayList<Unite>();
		
		HashSet<Unite> ennemiTemp = new HashSet<Unite>();
		
		for( int i = 0 ; i < populationPerso.size() ; i++ )
		{
			for( int j = 0 ; j < populationPerso.get(i).interne.size() ; j++ )
			{
				ennemiTemp.addAll( populationPerso.get(i).interne.get(j).getEnnemis() );
			}
		}
		
		ennemi.addAll(ennemiTemp);

		graphe.creationCarteInfluence( ennemi );

		for( int i = 0 ; i < populationPerso.size() ; i++ )
		{
			populationPerso.get(i).goOn( this );
			//populationPerso.get(i).goOn( this );
		}
		
		if( batimentPrinc != null && populationPerso.size() < 2 )
		{
			batimentPrinc.produit( 1 );
		}
	}

	public void retirerMort(ArrayList<Unite> populationMort)
	{
		ArrayList<Groupe> populationPersoMort = new ArrayList<Groupe>();
		
		for( int i = 0 ; i < populationPerso.size() ; i++ )
		{
			if( populationPerso.get(i).interne.removeAll(populationMort) )
			{
				if( populationPerso.get(i).interne.size() == 0 )
				{
					populationPersoMort.add(populationPerso.get(i));
				}
			}
		}
		
		populationPerso.removeAll(populationPersoMort);
	}

	public void ajouterBatiment(int type , int x, int y)
	{
		Groupe g = new Groupe();
		UniteBatiment u = new UniteBatiment(x,y, camp , motor );
		u.setMstate(new ModuleStateImpU( u , ModuleStateImp.CONSTRUCTION ) );

		g.add(u);
		g.setMs( new ModuleStateImpG( g , ModuleStateImp.GROUPE_STRUCT ));
		motor.ajoutUnite(u);
		populationPerso.add(g);
		batimentPrinc = u;
		graphe.ajouterSurfaceTempCircle( x , y, u.getSize() );
	}


	public void ajouterUnite( Unite u , int type, int x, int y)
	{
		if( type == 0 )
		{
			Groupe g = new Groupe();
			g.setMs( new ModuleStateImpG( g , ModuleStateImp.GROUPE_CIVIL ));
			
			u.setMstate(new ModuleStateImpU(u, ModuleStateImp.CIVIL ));
			u.setMobile(new ModuleMobilePerso( u ));
			g.add(u);
			
			populationPerso.add(g);
		}
		else if( type == 1 )
		{
			Groupe g = new Groupe();
			g.setMs( new ModuleStateImpG( g , ModuleStateImp.GROUPE_RECOL ));
			
			u.setMstate(new ModuleStateImpU(u, ModuleStateImp.RECOL ));
			u.setMobile(new ModuleMobilePerso( u ));
			g.add(u);
			
			populationPerso.add(g);
		}
	}

}